package com.syzg.base.module.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.syzg.common.config.SysProperties;
import com.syzg.common.controller.BaseController;
import com.syzg.common.model.WebInfo;
import com.syzg.common.util.SearchMap;
import com.syzg.common.util.MethodUtil;
import com.syzg.common.model.WebObj;
import com.syzg.common.util.PKCreator;
import com.syzg.common.exception.BaseException;
import com.syzg.core.page.PageResult;
import com.syzg.core.util.StringUtil;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.syzg.base.merge.model.Module;
import com.syzg.base.module.service.ModuleService;

@Controller
@RequestMapping("/path/module")
public class ModuleController extends BaseController{
	
	static Logger log = LoggerFactory.getLogger(ModuleController.class);
	
	@Resource
	private ModuleService moduleService;
	
	@RequestMapping("/listModule")
	public ModelAndView listModule(WebObj webObj)throws Exception{
		ModelAndView mv = new ModelAndView("/base/jsp/module/listModule.jsp");;
		SearchMap searchMap = SearchMap.forClass(Module.class);
		String title = webObj.getSearchValue().get("title");
		if(StringUtil.isNotEmpty(title)){
			searchMap.like("a.name", "%" + title + "%");
		}
		PageResult pageResult = moduleService.findObjectsForPage(webObj.getPageNo(), SysProperties.PAGESIZE, searchMap);
        webObj.setPageResult(pageResult);
        mv.addObject("webObj",webObj);
        return mv;
	}
	
	@RequestMapping("/initModule")
	public ModelAndView initModule() throws Exception {
		ModelAndView mv = new ModelAndView("/base/jsp/module/addModule.jsp");
		try {
			mv.addObject("module", new Module());
			/*LoginObject loginObj = SystemUtil.getLoginObject();
			this.module.setCreatePersonId(new String(loginObj.getPersonId()));
			this.module.setCreator(loginObj.getPersonName());
			this.module.setCreateTime(new Date());*/
			SearchMap searchMap = SearchMap.forClass(Module.class);
			searchMap.eq("a.href", "#");
	    	searchMap.notEq("a.id", "0");
	    	searchMap.addOrder("a.parentModule.id", SearchMap.ORDER_ASC);
	    	searchMap.addOrder("a.orderNo", SearchMap.ORDER_ASC);
	    	mv.addObject("moduleList", moduleService.findObjects(searchMap));
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/addModule")
	public @ResponseBody String addModule(Module module) throws Exception {
		WebObj webObj = new WebObj();
		try {
			/*LoginObject loginObj = SystemUtil.getLoginObject();
			this.module.setCreatePersonId(new String(loginObj.getPersonId()));
			this.module.setCreator(loginObj.getPersonName());
			this.module.setCreateTime(new Date());*/
			module.setId(PKCreator.getPk());
			if(module.getLeafFlag()==0){
				module.setHref("#");
				Module parentModule = new Module();
				parentModule.setId("0");
				module.setParentModule(parentModule);
			}
			moduleService.create(module);
			setControllerStatus(webObj,WebInfo.STATUS_SUCCESS);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			setControllerStatus(webObj,WebInfo.STATUS_FAILURE);
			webObj.getWebInfo().getErrorList().add("添加模块失败");
		}
		return new JSONObject(webObj).toString();
	}
	
	@RequestMapping("/viewModule")
	public ModelAndView viewModule(Module module,ModelAndView mv) throws Exception {
		mv.setViewName("/base/jsp/module/viewModule.jsp");
		try {
			String id = module.getId();
			Module moduleVO = moduleService.findObjectById(id);
			mv.addObject("module",moduleVO);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/editModule")
	public ModelAndView editModule(String id) throws Exception {
		ModelAndView mv = new ModelAndView("/base/jsp/module/editModule.jsp");
		try {
			Module module = moduleService.findObjectById(id);
			mv.addObject("module", module);
			WebObj webObj = new WebObj();
			mv.addObject("webObj",webObj);
			SearchMap searchMap = SearchMap.forClass(Module.class);
			searchMap.eq("a.href", "#");
	    	searchMap.notEq("a.id", "0");
	    	searchMap.addOrder("a.parentModule.id", SearchMap.ORDER_ASC);
	    	searchMap.addOrder("a.orderNo", SearchMap.ORDER_ASC);
	    	mv.addObject("moduleList", moduleService.findObjects(searchMap));
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/modModule")
	public @ResponseBody String modModule(String id,Module moduleVO) throws Exception {
		WebObj webObj = new WebObj();
		try {
			Module module = moduleService.findObjectById(id);
			module = (Module) MethodUtil.copyProperties(module, moduleVO);
			if(module.getLeafFlag()==0){
				module.setHref("#");
				Module parentModule = new Module();
				parentModule.setId("0");
				module.setParentModule(parentModule);
			}
			moduleService.update(module);
			setControllerStatus(webObj,WebInfo.STATUS_SUCCESS);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			setControllerStatus(webObj,WebInfo.STATUS_FAILURE);
			webObj.getWebInfo().getErrorList().add("编辑模块失败");
		}
		return new JSONObject(webObj).toString();
	}
	
	@RequestMapping("/delModule")
	public ModelAndView delModule(String id,WebObj webObj) throws Exception {
		String[] ids = webObj.getSelectedRow();
		try {
			if(ids == null) { //删除单个
				if(id == null) {
					ids = new String[0];
				} else {
					ids = new String[]{String.valueOf(id)};
				}
			}
			String[] idsStr = new String[ids.length];
			int i = 0;
			for(String idVO: ids) {
				idsStr[i++] = new String(idVO);
			}
			moduleService.delete(idsStr);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return listModule(webObj);
	}
}
